<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';
	/**
	 * @brief This class Describes the Secondary_Data Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 19 OCT 2011
	 * 	 - <b>Author</b>: Jack Farmer
	 * 	  - Initial Version
	 *
	 * @author Jack Farmer
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_Secondary_Data extends cTable
	{
		const C_INT_MAX_RESULTS_RETURNED = 500;
		const C_INT_DEFAULT_RESULTS_RETURNED = 100;
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = NULL;
		public $m_int_COL_ArticleID = NULL;
		public $m_int_COL_Type = NULL;
		public $m_int_COL_Data = NULL;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "Secondary Data";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID for each item.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_ArticleID = new cColVarChar("article_id");
			$obj_COL_ArticleID->set_Length(36);
			$obj_COL_ArticleID->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_ArticleID->set_Comment("This column holds the Article GUID.");
			$obj_COL_ArticleID->set_Label("Article GUID");

			$obj_COL_Type = new cColVarChar("type");
			$obj_COL_Type->set_Length(36);
			$obj_COL_Type->set_Index(abs_Column::c_int_INDEX_INDEX);
			$obj_COL_Type->set_Comment("This column holds the data type.");
			$obj_COL_Type->set_Label("Data type");

			$obj_COL_Data = new cColLongBlob("data");
			$obj_COL_Data->set_Comment("This column holds the data.");
			$obj_COL_Data->set_Label("Data");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//			$this->m_obj_Connection->activate_Logging();
			//			$this->m_obj_Connection->activate_LogDebug();
			//			$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_ArticleID = $this->add_Column($obj_COL_ArticleID);
			$this->m_int_COL_Type = $this->add_Column($obj_COL_Type);
			$this->m_int_COL_Data = $this->add_Column($obj_COL_Data);

		}
		static public function encode($str_Data)
		{
			$str_Data = base64_encode(serialize($str_Data));
			return $str_Data;
		}

		static public function decode($str_Data)
		{
			$str_Data = unserialize(base64_decode($str_Data));
			return $str_Data;
		}
		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}

			return false;
		}
		/**
		 * This function is used to update data that is already in the database
		 */
		public function update_Data2DB()
		{
			$this->clear_Update();
			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());

			return parent::update_Data2DB();
		}
		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());

			return parent::insert_Data2DB();
		}

		/**
		 *
		 * This function is used to delete Data from the database
		 * @param string $str_UserID
		 */
		public function delete_Query($str_ID)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
			if ($this->do_Delete() < 0)
			{
				return false;
			}
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the User to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			//print_r($str_ID);
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the Query to check (GUID)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}
	}
?>
